package cn.nawang.ebeim.ui.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import cn.nawang.ebeim.client.bean.TbStorageStrategy;
import cn.nawang.ebeim.client.util.ApiUtil;

public class DirectoryUtil {

  public static File[] getFiles(String parent, boolean isRoot) {
    if (isRoot) {
      return getSystemRoots();
    }
    
    File file = new File(parent);
    if (file.exists()) {
      List<File> fList = new ArrayList<File>();
      File[] fArr = file.listFiles();
      for (int i = 0; i < fArr.length; i++) {
        File f = fArr[i];
        if(!f.isHidden()){
          fList.add(f);
        }
      }
      File[] rs = new File[fList.size()];
      for (int i = 0; i < fList.size(); i++) {
        rs[i] = fList.get(i);
      }
      return rs;
    }
    
    return new File[0];
  }
  
  public static File[] getSystemRoots() {
    javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView(); 
    File f = fsv.getHomeDirectory();
    File[] roots = File.listRoots();
    File[] fs = new File[roots.length+1];
    fs[0] = f;
    for (int i = 1; i < roots.length + 1; i++) {
      fs[i] = roots[i-1];
    }
    return fs;
  }
  
  public static File[] getDirs(String parent, boolean isRoot) {
    if (isRoot) {
      return getSystemRoots();
    }
    
    File file = new File(parent);
    if (file.exists() && file.isDirectory()) {
      List<File> fList = new ArrayList<File>();
      File[] fArr = file.listFiles();
      for (int i = 0; i < fArr.length; i++) {
        File f = fArr[i];
        if(!f.isHidden() &&f.isDirectory() && f.listFiles()!=null){
          fList.add(f);
        }
      }
      File[] rs = new File[fList.size()];
      for (int i = 0; i < fList.size(); i++) {
        rs[i] = fList.get(i);
      }
      return rs;
    }
    
    return new File[0];
  }
  
  
  public void run() {
    FileSystemView fs = FileSystemView.getFileSystemView();
    //File[] roots = File.listRoots();
    File file = new File("F:\\");
    File[] roots = file.listFiles();
    for (File f: roots) {
      System.out.println(f.getName());
    }    
  }
  
  public static void main(String[] args) {
//    DirectoryUtil util = new DirectoryUtil();
//    util.run();

  }

}
